﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using BrainTechLLC;
using BrainTechLLC.WPF;
using System.Windows.Data;
using System.Diagnostics;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Windows;
using System.Collections.Specialized;
using DirectoryViewModels;
using System.Threading;
using BrainTechLLC.ThreadSafeObjects;

namespace BrainTechLLC.FolderExplorer
{
    public partial class FolderViewModel : BaseNotifyPropertyChangedUI
    {
        protected bool _Complete;
        public bool Complete
        {
            get { return _Complete; }
            set
            {
                if (_Complete != value)
                {
                    _Complete = value;
                    OnPropertyChanged("Complete");
                }
            }
        }

        public void FindFoldersWithoutCachedSubfolders(List<FolderViewModel> results, List<FolderViewModel> complete, int limit)
        {
            if (_Complete)
            {
                return;
            }

            if (!IsVisible && Depth >= 5)
            {
                return;
            }

            if (this.SubFoldersCached == null)
            {
                results.Add(this);
            }
            else
            {
                for (int i = 0; i < this.SubFoldersCached.Count; i++)
                {
                    FolderViewModel subFolder = this.SubFoldersCached[i];
                    subFolder.FindFoldersWithoutCachedSubfolders(results, complete, limit);
                    if (results.Count > limit)
                    {
                        break;
                    }
                }

                if (results.Count == 0)
                {                    
                    this.Complete = true;
                    complete.Add(this);
                }
            }
        }

        public int PrefetchData(double secondsForTopPass, double secondsForSecondPass)
        {
            DateTime dt = DateTime.Now;

            if (this.SubFolders.Count == 0)
            {
                return 0;
            }

            int prefetches = 0;

            // prefetch a bit of info
            for (int n = 0; n < this.SubFolders.Count; n++)
            {
                var folder = this.SubFolders[n];

                if (folder.SubFoldersCached == null)
                {
                    prefetches++;                    
                    folder.ReadFoldersOnDemand(Int32.MaxValue, ref folder._subFoldersForUIBinding, ref folder._subFolderList);
                }

                if (dt.AddSeconds(secondsForTopPass) < DateTime.Now)
                {
                    break;
                }
            }

            dt = DateTime.Now;

            for (int n = 0; n < this.SubFolders.Count; n++)
            {
                var folder = this.SubFolders[n];

                if (folder.SubFoldersCached != null)
                {
                    for (int i = 0; i < folder.SubFoldersCached.Count; i++)
                    {
                        var subFolder = folder.SubFoldersCached[i];

                        if (subFolder.SubFoldersCached == null)
                        {
                            prefetches++;
                            subFolder.ReadFoldersOnDemand(Int32.MaxValue, ref subFolder._subFoldersForUIBinding, ref subFolder._subFolderList);
                        }

                        if (dt.AddSeconds(secondsForSecondPass) < DateTime.Now)
                        {
                            break;
                        }
                    }
                }
            }

            //if (prefetches > 0)
            //{
            //    Console.WriteLine(string.Format("Prefetches: {0}", prefetches));
            //}
            return prefetches;
        }
    }
}
